//
//  CalcViewController.m
//  Calc
//
//  Created by 黄 晨 on 11-12-19.
//  Copyright 2011年 北京火星时代科技有限公司. All rights reserved.
//

#import "ScCalcViewController.h"

@implementation ScCalcViewController

@synthesize display;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// the designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
	{
        self.wantsFullScreenLayout = YES; // we want to overlap the status bar.
        
		// when presented, we want to display using a cross dissolve
		self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    oldStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:oldStatusBarStyle animated:NO];    
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // return YES for supported orientations
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

#pragma mark -
#pragma mark Main Functions
// 点击数字
- (IBAction) numberClicked:(id)sender
{
    UIButton *btn = (UIButton *) sender;
    int tag = [btn tag];
    
    if([[display text] length] <= 10)
    {
        // 如果点击的是 .
        if (kDotTag == tag) 
        {
            dotOffset = 0 == dotOffset ? 1 : dotOffset;
        }
        // 如果点击的是数字
        else
        {
            if (0 == dotOffset) 
            {
                int number = [[display text] intValue];
                number = number*10 + tag;
                [display setText:[[NSString alloc] initWithFormat:@"%d", number]];
            }
            else
            {
                double number = [[display text] doubleValue];
                number = number + tag * pow(0.1, dotOffset++);
                [display setText:[[NSString alloc] initWithFormat:@"%g", number]];
            }
            
        }
    }
}

// 点击操作
- (IBAction) operaterClicked:(id)sender
{
    UIButton *btn = (UIButton *) sender;
    Operator op = [btn tag];
    switch (op) {
        // 等于
        case equal:
            if (0 != leftOperand && 0 != operater) {
                leftOperand = [self calculate:operater rightOperand:[[display text] doubleValue]];
                [display setText:[[NSString alloc] initWithFormat:@"%g", leftOperand]];
                operater = 0;
            }
            break;
            
        // 重置   
        case reset:
            [self clear:sender];
            break;
            
        // 设置左操作数
        default:
            leftOperand = [[display text] doubleValue];
            [display setText:@"0"];
            operater = op;
            dotOffset = 0;
            break;
    }
}

// 计算
- (double) calculate:(Operator)op rightOperand:(double) rightOperand
{
    double result = 0;
    
    switch (op) {
        // 加    
        case plus:
            result = leftOperand + rightOperand;
            break;
        // 减   
        case minus:
            result = leftOperand - rightOperand;
            break;
        // 乘   
        case times:
            result = leftOperand * rightOperand;
            break;
        // 除   
        case divide:
            if (0 == rightOperand) 
            {
                [self divideZero];
            }
            else
            {
                result = leftOperand / rightOperand;
            }
            break;
            
        default:
            break;
    }
    return result;
}

// 清除结果及操作
- (void) clear:(id)sender
{
    [display setText:@"0"];
    leftOperand = 0;
    operater = 0;
    dotOffset = 0;
}

// 显示被除数为零错误
- (void) divideZero 
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error！" 
													message:@"Divisor must not be zero！"
												   delegate:self 
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
}


// 点击内存操作
- (IBAction) memoryOperaterClicked:(id)sender
{
    UIButton *btn = (UIButton *) sender;
    Operator op = [btn tag];
    switch (op) {
            // M+
        case mplus:
            [self updateMemoryValue:[[display text] doubleValue] operate:@"plus"];
            break;
            
            // M-
        case minus:
            [self updateMemoryValue:[[display text] doubleValue] operate:@"minus"];
            break;
            
            // MR
        case mread:
            [self readMemoryValue];
            break;
            
        default:
            break;
    }
}

// 读取存储的数据
- (void) readMemoryValue
{
    if (0 == leftOperand) {
        leftOperand = mValue;
    }
    [display setText:[[NSString alloc] initWithFormat:@"%g", mValue]];
}

// 修改存储的值
- (void) updateMemoryValue:(double)value operate:(NSString *)op
{
    if ([op isEqualToString:@"plus"]) {
        mValue += value;
    }
    else if([op isEqualToString:@"minus"]){
        mValue -= value;
    }
}

// 点击函数
- (IBAction)functionClicked:(id)sender
{

}

- (void)dealloc {
    [super dealloc];
}

@end
